package com.randao.manage.firstPage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.randao.framework.common.model.response.CommonCode;
import com.randao.framework.common.model.response.QueryResponseResult;
import com.randao.framework.common.model.response.QueryResult;
import com.randao.framework.model.domain.house.Houses;
import com.randao.framework.model.domain.productImg.ProductImg;
import com.randao.manage.firstPage.dao.HousesManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-04月-16日  15:46
 * Look at the wine in the glass toast the rich first.
 */
@Service
public class HousesManageService {

    @Autowired
    private HousesManageMapper housesManageMapper;


    //@Autowired
    //private HousesTestRepository housesTestRepository;

    /**
     * 查询全部楼盘
     * @return
     */
    public QueryResponseResult<Houses> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Houses> housesPage = housesManageMapper.findAll(pageNum, pageSize);
        List<Houses> housesList = housesPage.getResult();
        long total = housesPage.getTotal();

        QueryResult<Houses> qr = new QueryResult<>();
        qr.setList(housesList);
        qr.setTotal(total);

        return new QueryResponseResult<>(CommonCode.SUCCESS, qr);

    }

    //public List<Houses> findAllTest(){
    //    return housesTestRepository.findAll();
    //}

    /**
     * 根据id查询楼盘
     *
     * @param hid   string类型
     * @param fnum
     * @return
     * @throws Exception
     */
    public List<Houses> findByHidAndFloor(String hid, String fnum) {
        int housesId = Integer.parseInt(hid);
/*        if (fnum==null || "".equals(fnum)){
            fnum = "1";
        }*/
        return housesManageMapper.findByHidAndFloor(housesId, fnum);

    }

    /**
     * 根据楼层筛选房源
     *
     * @param floor
     * @return
     */
    public List<Houses> findByFloor(String floor) {
        return housesManageMapper.findByFloor(floor);
    }

    /**
     * 根据房源id查询房源详情
     *
     * @param id
     * @return
     */
    public Houses findByHid(String id) {
        int hid = Integer.parseInt(id);
        return housesManageMapper.findByHid(hid);
    }

    /**
     * 展示图片
     *
     * @param hid
     * @return
     */
    public List<ProductImg> findProductImg(String hid) {
        Integer id = 0;
        List<ProductImg> imgList;
        if (hid != null && !"".equals(hid)) {
            id = Integer.parseInt(hid);
        }
        //查询
        imgList = housesManageMapper.findProductImg(id);

        return imgList;
    }

}
